var inps = document.getElementsByTagName("input");

for (var i = 0; i < inps.length - 2; i++) {
    inps[i].onfocus = function () {
        if (this.name == "phone") {
            this.nextElementSibling.innerHTML = "请输入正确的手机号"
            this.nextElementSibling.style.color = "#ccc";
        } else if (this.name == "pwd") {
            this.nextElementSibling.innerHTML = "请输入4-8位大写字母或小写字母或数字，必须是三者的组合"
            this.nextElementSibling.style.color = "#ccc";

        }
        else if (this.name == "yzm") {
            this.nextElementSibling.innerHTML = "请输入正确的验证码"
            this.nextElementSibling.style.color = "#ccc";
        }
        else if (this.name == "mingzi") {
            this.nextElementSibling.innerHTML = "请输入昵称"
            this.nextElementSibling.style.color = "#ccc";
        }
        // this.nextElementSibling.style.color = "#ccc";

    }

    inps[i].onblur = function () {
        var user = this.value;
        if (this.name == "phone") {
            var reg = /^(\+86|0086)?\s*1[3-9]\d{9}$/;
        } else if (this.name == "pwd") {
            var reg = /^(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,8}$/;
        }
        else if (this.name == "mingzi") {
            var reg = /[^.]/
        }


        if (this.name == "yzm") {
            if (inps[3].value == inps[4].value&&inps[3].value!="") {
                
                this.nextElementSibling.innerHTML = "正确";
                this.nextElementSibling.style.color = "green";
            } else {
                this.nextElementSibling.innerHTML = "错误";
                this.nextElementSibling.style.color = "red";
                this.style.border = "red 2px solid";
            }
           


        }

        if (reg) {
            var bool = reg.test(user);

            if (bool) {
                this.nextElementSibling.innerHTML = "正确";
                this.nextElementSibling.style.color = "green";
            }
            else {
                this.nextElementSibling.innerHTML = "错误";
                this.nextElementSibling.style.color = "red";
                this.style.border = "red 2px solid";
            }

            if (user == "") {
                this.nextElementSibling.innerHTML = "不能为空";
                this.nextElementSibling.style.color = "red";
                this.style.border = "red 2px solid";
            }

        }




    }
}
var form = document.getElementsByTagName("form")[0];
form.onsubmit = function () {
    inps[0].onblur();
    inps[1].onblur();
    inps[2].onblur();
    inps[3].onblur();
    for (var i = 0; i < inps.length; i++) {
        if (inps[i].nextElementSibling.style.color == "red") {
            return false;
        }
    }
}


// inps[0].oninput = function () {
//     console.log(this.value);
// }

function suijishu() {
    function f1(a, b) {
        for (var i = a; i <= b; i++) {
            arr.push(String.fromCharCode(i))
        }
    }
    var arr = [];
    f1(48, 52);
    f1(65, 90);
    f1(97, 122);

    var codes = "";

    while (codes.length < 4) {
        var r = parseInt(Math.random() * (arr.length));
        if (codes.indexOf(arr[r]) == -1) {
            codes += arr[r];
        }
    }
    inps[4].value = codes
}
